﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Com.CMS263.PO;
using Com.CMS263.BLL;
using System.Collections.Generic;
using System.IO;
using Com.CMS263.Util;

namespace Com.CMS263.Web.manage.ManageCommunity
{
    public partial class community_edit : System.Web.UI.Page
    {
        RegionBLL rbll = new RegionBLL();
        CommunityBLL cbll = new CommunityBLL();
        public IList<Region> regionList = new List<Region>();
        public Community entity = new Community();
        int id = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            regionList = rbll.GetListPaging(0, -1);
            id = Convert.ToInt32(Request["id"]);
            entity = (Community)cbll.FindModelById("Community", id);
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            int id = Convert.ToInt32(Request["hdID"]);

            Community community = new Community();
            community = (Community)cbll.FindModelById("Community", id);
            string name = Request["txtName"];
            int region = Convert.ToInt32(Request["selRegion"]);
            string regionArea = Request["selRegionArea"];
            int price = Convert.ToInt32(Request["txtPrice"]);
            string year = Request["txtYear"];
            string build = Request["txtBuild"];
            string developer = Request["txtDeveloper"];
            string addres = Request["txtAddress"];
            string intro = Request["intro"];
            string map = Request["map"];
            string val = "";
            //文件按天存储，没有目录就自己创建
            string strDay = DateTime.Now.ToString("yyyy-MM-dd");
            string savePath = Server.MapPath("/ArtUpFile/" + strDay);
            if (!Directory.Exists(savePath))
            {
                Directory.CreateDirectory(savePath);
            }
            HttpPostedFile hpf = Request.Files["fileCover"];
            //string ext = hpf.FileName.Substring(hpf.FileName.LastIndexOf('.'));
            //string okext = "jpg,gif,jpeg,png";
            //if (okext.IndexOf(ext.ToLower()) < 0)
            //{
            //    StringUtil.Alert("图片类型请上传" + okext, "-1");
            //}
            if (hpf.ContentLength < 5)
            {
                
            }
            else
            {
                val = strDay + "/" + UpLoadFileUtil.UpLoadFile(Request.Files["fileCover"], savePath);
                community.Cover = val;
            }
            community.Address = addres;
            community.Buildtype = build;
            
            community.Developer = developer;
            community.Intro = intro;
            community.Map = map;
            community.Name = name;
            community.Price = price;
            Region re = (Region)rbll.FindModelById("Region",region);
            community.Region = re.Name;
            community.Regionarea = regionArea;
            community.Year = year;
            cbll.entity = community;
            if (cbll.UpDate())
            {
                StringUtil.Alert("修改成功", "communityList.aspx");
            }
            else
            {
                StringUtil.Alert("修改失败", "-1");
            }
        }
    }
}
